import json
import os

with open('config.json', 'r', encoding='utf8') as f:
    data = json.loads(f.read())
    download_dir = data.get('download_dir')
    m3u8_download_program_path = data.get('m3u8_download_program_path')
    m3u8_cmd_args = data.get('m3u8_cmd_args')
    aria2c_program_path = data.get('aria2c_program_path')
    aria2c_cmd_args = data.get('aria2c_cmd_args')


def get_download_dir() -> str:
    """
    获取下载目录
    :param 下载目录
    """
    return download_dir


def run_command(command: str) -> bool:
    """
    执行命令
    :param 命令
    :return 是否执行成功
    """
    return os.system(command) == 0


def aria2c_download(url, work_dir: str, filename: str):
    """
    aria2c 下载接口
    :param url: 下载链接
    :param work_dir: 下载目录
    :param filename: 文件名(含拓展名)
    :return True 执行成功, False 下载失败
    """
    return run_command(f'{aria2c_program_path} {aria2c_cmd_args} --dir "{work_dir}" -c -o "{filename}" {url}')


def m3u8_download(url, work_dir: str, filename: str):
    """
    m3u8 下载接口
    :param url: m3u8文件下载链接
    :param work_dir: 下载目录
    :param filename: 文件名(不含拓展名)
    """
    return run_command(f'{m3u8_download_program_path} -u={url} -sp={work_dir} {m3u8_cmd_args} -o={filename}')
